package com.woniuxy.service.impl;

import com.woniuxy.dao.AuthDao;
import com.woniuxy.entity.User;
import com.woniuxy.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    AuthDao authDao;

    @Override
    @Cacheable(value = "user", key = "#user.getUsername()")
    public User findUser(User user) {
        return authDao.findByUsername(user.getUsername());
    }

    @Override
    @CachePut(value = "user", key = "#user.getUsername()")
    public User updateUser(User user) {
        return authDao.save(user);
    }

    // allEntries：true 清除所有user::*的缓存记录，key="admin"无效
    @Override
    @CacheEvict(value = "user", key = "admin", allEntries = true, beforeInvocation = true)
    public void cleatCache() {
        // beforeInvocation：true， 即使报错也能清除所有缓存
        int i = 1/0;
    }
}
